package net.ib.mtalk.ui.buddy.info;

import java.util.ArrayList;
import java.util.List;

import net.ib.mntalk.protocol.Common.BuddyInfo;
import net.ib.mtalk.R;
import net.ib.mtalk.database.DatabaseContract.ContactTable;
import net.ib.mtalk.ui.buddy.item.BuddyProfileItem;
import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class BuddyInfoAdapter extends BaseAdapter{
	private LayoutInflater inflater;
	private int mPhoneLayout;
	private Context mContext;
	List<BuddyProfileItem> arrayItem = new ArrayList<BuddyProfileItem>();
	public static int PHONE = 0;
	public static int EMAIL = 1;
	
	public BuddyInfoAdapter(Context context, String msb, String lsb){
		mContext = context;
		inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mPhoneLayout = R.layout.list_item_phone;
		int rawcontact_id = 0;
		
//		String select = "((" + BuddyTable.KEY_BUDDY_UUID_MSB + "=?) AND (" + BuddyTable.KEY_BUDDY_UUID_LSB + "=?))"; 
//		String[] selectionArgs = new String[] {msb, lsb};
//		Cursor cursor = mContext.getContentResolver().query(BuddyTable.CONTENT_URI, BuddyTable.PROJECTION, select, selectionArgs, null);
		String select = "((" + ContactTable.KEY_BUDDY_UUID_MSB + "=?) AND (" + ContactTable.KEY_BUDDY_UUID_LSB + "=?))"; 
		String[] selectionArgs = new String[] {msb, lsb};
		Cursor cursor = mContext.getContentResolver().query(ContactTable.CONTENT_URI, ContactTable.PROJECTION, select, selectionArgs, null);
		try{
			if(cursor.moveToFirst()) {
				rawcontact_id = cursor.getInt(cursor.getColumnIndexOrThrow(ContactTable.KEY_CONTACT_PHONE_ID));
			}
		} finally{
			cursor.close();
		}
		
		List<String> list = getBuddyPhoneList(mContext);

		Cursor phoneCursor = mContext.getContentResolver().query(Phone.CONTENT_URI, null, Phone.RAW_CONTACT_ID + "=" + rawcontact_id, null, null);
		try{
			while(phoneCursor.moveToNext()){
				String number = phoneCursor.getString(phoneCursor.getColumnIndex(Phone.NUMBER));
				String numberType = phoneCursor.getString(phoneCursor.getColumnIndex(Phone.TYPE));
				arrayItem.add(new BuddyProfileItem(PHONE, numberType, number, list.contains(number), "", ""));
			}			
		} finally{
			phoneCursor.close();
		}
			
		Cursor emailCursor = mContext.getContentResolver().query(Email.CONTENT_URI, new String[]{Email.DATA, Email.TYPE},
				Email.RAW_CONTACT_ID + "=" + rawcontact_id, null, null);
		try{
			while(emailCursor.moveToNext()) {
				String email = emailCursor.getString(emailCursor.getColumnIndex(Email.DATA));
				String emailType = emailCursor.getString(emailCursor.getColumnIndex(Email.TYPE));
				arrayItem.add(new BuddyProfileItem(EMAIL, "", "", list.contains(email), emailType, email));
			}
		} finally {
			emailCursor.close();
		}
	}
	
	@Override
	public int getCount() {
		return arrayItem.size();
	}
	@Override
	public BuddyProfileItem getItem(int position) {
		return arrayItem.get(position);
	}
	@Override
	public long getItemId(int position) {
		return position;
	}
	@Override
	public int getItemViewType(int position) {
		return arrayItem.get(position).Type;
	}
	@Override
	public int getViewTypeCount() {
		return 2;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if(convertView == null){
			int res = 0;
			res = mPhoneLayout;

			convertView = inflater.inflate(res, parent, false);
			holder = new ViewHolder((RelativeLayout) convertView);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		switch (arrayItem.get(position).Type) {
			case 0:
				String ptype = null;
				ptype = switchPhoneType(position, ptype);
				holder.phonetext.setText(ptype);
				holder.phonenum.setText(arrayItem.get(position).phoneNumber);
				if(arrayItem.get(position).mTalk) holder.imgviewMTalk.setVisibility(View.VISIBLE);
				else holder.imgviewMTalk.setVisibility(View.GONE);
				break;
			case 1:
				String etype = null;
				etype = switchEmailType(position, etype);
				holder.phonetext.setText(etype);
				holder.phonenum.setText(arrayItem.get(position).emailAddress);
				if(arrayItem.get(position).mTalk) holder.imgviewMTalk.setVisibility(View.VISIBLE);
				else holder.imgviewMTalk.setVisibility(View.GONE);
				break;
		}
		convertView.setTag(holder);
		return convertView;
	}

	private String switchEmailType(int position, String etype) {
		switch(Integer.parseInt(arrayItem.get(position).emailType)){
			case Email.TYPE_HOME :
				etype = mContext.getString(R.string.vcard_email_type_home);
				break;
			case Email.TYPE_WORK :
				etype = mContext.getString(R.string.vcard_email_type_work);
				break;
			case Email.TYPE_OTHER :
				etype = mContext.getString(R.string.vcard_email_type_other);
				break;
			case Email.TYPE_MOBILE :
				etype = mContext.getString(R.string.vcard_email_type_mobile);
				break;
			case Email.TYPE_CUSTOM :
				etype = mContext.getString(R.string.vcard_email_type_custom);
				break;
			
			default:
			break;
		}
		return etype;
	}

	private String switchPhoneType(int position, String ptype) {
		switch(Integer.parseInt(arrayItem.get(position).phoneType)){
			case Phone.TYPE_HOME :
				ptype = mContext.getString(R.string.vcard_phone_type_home);
				break;
			case Phone.TYPE_MOBILE :
				ptype = mContext.getString(R.string.vcard_phone_type_mobile);
				break;
			case Phone.TYPE_WORK :
				ptype = mContext.getString(R.string.vcard_phone_type_work);
				break;
			case Phone.TYPE_FAX_WORK :
				ptype = mContext.getString(R.string.vcard_phone_type_fax_work);
				break;
			case Phone.TYPE_FAX_HOME :
				ptype = mContext.getString(R.string.vcard_phone_type_fax_home);
				break;
			case Phone.TYPE_PAGER :
				ptype = mContext.getString(R.string.vcard_phone_type_pager);
				break;
			case Phone.TYPE_OTHER :
				ptype = mContext.getString(R.string.vcard_phone_type_other);
				break;
			case Phone.TYPE_CALLBACK :
				ptype = mContext.getString(R.string.vcard_phone_type_callback);
				break;
			case Phone.TYPE_CAR :
				ptype = mContext.getString(R.string.vcard_phone_type_car);
				break;
			case Phone.TYPE_COMPANY_MAIN :
				ptype = mContext.getString(R.string.vcard_phone_type_company_main);
				break;
			case Phone.TYPE_ISDN :
				ptype = mContext.getString(R.string.vcard_phone_type_isdn);
				break;
			case Phone.TYPE_MAIN :
				ptype = mContext.getString(R.string.vcard_phone_type_main);
				break;
			case Phone.TYPE_OTHER_FAX :
				ptype = mContext.getString(R.string.vcard_phone_type_other_fax);
				break;
			case Phone.TYPE_RADIO :
				ptype = mContext.getString(R.string.vcard_phone_type_radio);
				break;
			case Phone.TYPE_TELEX :
				ptype = mContext.getString(R.string.vcard_phone_type_telex);
				break;
			case Phone.TYPE_TTY_TDD :
				ptype = mContext.getString(R.string.vcard_phone_type_tty_tdd);
				break;
			case Phone.TYPE_WORK_MOBILE :
				ptype = mContext.getString(R.string.vcard_phone_type_work_mobile);
				break;
			case Phone.TYPE_WORK_PAGER :
				ptype = mContext.getString(R.string.vcard_phone_type_work_pager);
				break;
			case Phone.TYPE_ASSISTANT :
				ptype = mContext.getString(R.string.vcard_phone_type_assistant);
				break;
			case Phone.TYPE_MMS :
				ptype = mContext.getString(R.string.vcard_phone_type_mms);
				break;
			default:
			break;
		}
		return ptype;
	}

	private static class ViewHolder{
		RelativeLayout item;
		TextView phonetext;
		TextView phonenum;
		ImageView imgviewMTalk;
		
		public ViewHolder(RelativeLayout item){
			phonetext = (TextView) item.findViewById(R.id.phonetext);
			phonenum = (TextView) item.findViewById(R.id.phonenum);
			imgviewMTalk = (ImageView) item.findViewById(R.id.imgviewMTalk);
			this.item = item;
		}
	}
	
	private List<String> getBuddyPhoneList(Context context) {
		List<String> buddyPhoneList = new ArrayList<String>();
		
		String selection = ContactTable.KEY_BUDDY_UUID_MSB + " NOT NULL";
		Cursor cursor = context.getContentResolver().query(ContactTable.CONTENT_URI, new String[]{ContactTable.KEY_CONTACT_NUMBER}, selection, null, null);
		try{
			while(cursor.moveToNext()){
				buddyPhoneList.add(cursor.getString(cursor.getColumnIndexOrThrow(ContactTable.KEY_CONTACT_NUMBER)));
			}
		} finally{
			cursor.close();
		}
		return buddyPhoneList;
	}
}
